#include <Arduino.h>
#include <U8g2lib.h>

U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 16, /* data=*/ 17);   // ESP32 Thing, HW I2C with pin remapping

int sensorpin = A6; // Analog input pin that the sensor is attached to
int ledPin = 4;    // LED port
int sensorValue = 0;        // value read from the port
int outputValue = 0;        // value output to the PWM (analog out)
const int buttonPin = 6;     // the number of the pushbutton pin
int tonepin=5;

int buttonState = 0;         // variable for reading the pushbutton status

int t=0;
int sensorNum=0;
int screenNum=0;
int stage=0;
int score=0;

int barrier[10][2];
int barrierNum=0;

int pos[3];

void setup(void) {
  pinMode(tonepin,OUTPUT);
  pinMode(ledPin,OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(sensorpin, INPUT);
  Serial.begin(9600);
  u8g2.begin();
  u8g2.enableUTF8Print();       // enable UTF8 support for the Arduino print() function

  for(int i=0;i<10;i++){
    barrier[i][0]=140;
    barrier[i][1]=-1;
  }
  pos[0]=30;
  pos[1]=20;
  pos[2]=10;
}
void loop(void) {
  buttonState = digitalRead(buttonPin);
  sensorValue = analogRead(sensorpin);
  Serial.println(sensorValue);
  if(sensorNum>0){
    sensorNum--;
  }else{
    if (sensorValue < 200) {
    }
    else {
      sensorNum=100;
    }
  }

  if(screenNum>=0){
    screenNum--;
  }else{
    if(stage==0){
      u8g2.setFont(u8g2_font_unifont_t_symbols);  // use chinese2 for all the glyphs of "你好世界"
      u8g2.firstPage();
      do {
        u8g2.setCursor(15,20);//60 57
        u8g2.print("press button");     // Chinese "Hello World"
        u8g2.setCursor(15,40);//60 57
        u8g2.print("to play game"); 
      } while ( u8g2.nextPage() );
      delay(20);
      if(buttonState == HIGH){
        stage=1;
      }



      
    }else if(stage==1){
      //set barrier
      if(t%40==0){//create barrier
        barrier[barrierNum][0]=t+135;
        barrier[barrierNum][1]=t%3;
        barrierNum++;
        barrier[barrierNum][0]=t+135;
        barrier[barrierNum][1]=(t+1)%3;
        barrierNum++;
        barrierNum%=10;
      }
      
  
      //set my position

      int mystage=0;
        if(sensorNum>0 && buttonState == HIGH){
          mystage=2;
        }else if(buttonState == HIGH){
          mystage=1;
        }else{
          mystage=0;
        }
      int mypos=pos[mystage];
        
        
      //set image
      u8g2.setFont(u8g2_font_unifont_t_symbols);  // use chinese2 for all the glyphs of "你好世界"
      u8g2.firstPage();
      do {
        u8g2.setCursor(60, 57);//60 57
        u8g2.print("☃:");     // Chinese "Hello World"
        u8g2.print(score);
        for(int i=0;i<10;i++){
          if(barrier[i][1]==-1){
            continue;
          }
          u8g2.drawCircle(barrier[i][0]-t, pos[barrier[i][1]], 5, U8G2_DRAW_ALL);
        }
        u8g2.drawDisc(25, mypos, 5, U8G2_DRAW_ALL);
      } while ( u8g2.nextPage() );

      //game over verdict
      digitalWrite(ledPin, LOW);
      for(int i=0;i<10;i++){
          if(barrier[i][1]==-1){continue;}
          if(barrier[i][1]==mystage){
            if(barrier[i][0]-t<=33&&barrier[i][0]-t>=17){
              tone(tonepin,261);
              delay(1000);
              noTone(tonepin);
              stage=2;
            }
          }else if(barrier[i][0]-t==25){
              score++;
              digitalWrite(ledPin, HIGH); 
          }
      }

      
      t++;
      screenNum=20;



      
    }else if(stage==2){
      u8g2.setFont(u8g2_font_unifont_t_symbols);  // use chinese2 for all the glyphs of "你好世界"
      u8g2.firstPage();
      do {
        u8g2.setCursor(25,20);//60 57
        u8g2.print("GAME OVER");     // Chinese "Hello World"
        u8g2.setCursor(15,40);
        u8g2.print("press button");     // Chinese "Hello World"
        u8g2.setCursor(30,60);
        u8g2.print("continue");     // Chinese "Hello World"
      } while ( u8g2.nextPage() );
      delay(20);
      
      if(buttonState == HIGH){
        for(int i=0;i<10;i++){
          barrier[i][0]=140;
          barrier[i][1]=-1;
        }
        t=0;
        score=0;
        stage=1;
      }
    }else{
      stage=0;
    }
  }

}
